﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

public class PackItemMgr
{
    private readonly Dictionary<long, Item> Items;

    public PackItemMgr()
    {
        Items = new Dictionary<long, Item>();
    }

    /// <summary>
    /// 添加
    /// </summary>
    public void Add(Item item)
    {
        Items.Add(item.Id, item);
    }

    /// <summary>
    /// 获取
    /// </summary>
    public bool TryGet(long id, out Item item)
    {
        return Items.TryGetValue(id, out item);
    }

    /// <summary>
    /// 获取
    /// </summary>
    /// <param name="sid">静态id</param>
    /// <param name="outItem"></param>
    /// <returns></returns>
    public bool TryGetBySId(long sid, out Item outItem)
    {
        foreach (var item in Items.Values)
        {
            if (item.TableId == sid)
            {
                outItem = item;
                return true;
            }
        }

        outItem = null;
        return false;
    }

    /// <summary>
    /// 获取数量
    /// </summary>
    public int GetCount(long id)
    {
        foreach (var item in Items.Values)
        {
            if (item.TableId == id)
            {
                return item.Count;
            }
        }

        return 0;
    }

    /// <summary>
    /// 移除
    /// </summary>
    public void Remove(long id)
    {
        Items.Remove(id);
    }

    /// <summary>
    /// 获取所有
    /// </summary>
    public List<Item> GetAll()
    {
        return Items.Values.ToList();
    }
}
